//
//  ResultTableViewCell.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/7.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "ResultTableViewCell.h"

@interface ResultTableViewCell ()
{
    UILabel * titleLabel;
    UILabel * priceLabel;
    UILabel * timeLabel;
    UIImageView * avatarImage;
    UILabel * nameLabel;
    UILabel * personType;
    UILabel * tags;
    
    UILabel * w1;
    UILabel * w2;
    UILabel * w3;
    UILabel * w4;
    UILabel * w5;
    UILabel * w6;
    UILabel * w7;

    UIView * line;
}
@end

@implementation ResultTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 20)];
        titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [self addSubview:titleLabel];
        
        priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 20)];
        priceLabel.font = [UIFont boldSystemFontOfSize:15];
        priceLabel.textAlignment = NSTextAlignmentRight;
        [self addSubview:priceLabel];
        
        timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, ScreenWidth, 20)];
        timeLabel.font = [UIFont systemFontOfSize:12];
        timeLabel.textColor = [UIColor colorFromHexCode:@"b3b3b3"];
        [self addSubview:timeLabel];
        
        avatarImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 95, 40, 40)];
        [self addSubview:avatarImage];
        
        nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 95, ScreenWidth, 40)];
        nameLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:nameLabel];
        
        personType = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth - 60, 105, 40, 20)];
        personType.textAlignment = NSTextAlignmentCenter;
        personType.font = [UIFont systemFontOfSize:15];
        personType.backgroundColor = themeColor;
        personType.layer.cornerRadius = 4;
        personType.clipsToBounds = YES;
        personType.textColor = [UIColor whiteColor];
        [self addSubview:personType];
        
        tags = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth - 70, 40, 60, 20)];
        tags.textAlignment = NSTextAlignmentRight;
        tags.font = [UIFont systemFontOfSize:12];
        tags.layer.cornerRadius = 5;
        tags.textColor = [UIColor whiteColor];
        tags.backgroundColor = [UIColor colorFromHexCode:@"b3b3b3"];
        tags.layer.cornerRadius = 5;
        tags.textAlignment = NSTextAlignmentCenter;
        [self addSubview:tags];
        
        w1 = [[UILabel alloc]initWithFrame:CGRectMake(10, 70, 35, 15)];
        w1.font = [UIFont systemFontOfSize:12];
        w1.layer.cornerRadius = 2;
        w1.layer.borderColor = themeColor.CGColor;
        w1.layer.borderWidth = 1;
        w1.textAlignment = NSTextAlignmentCenter;
        w1.clipsToBounds = YES;
        w1.textColor = themeColor;
        [self addSubview:w1];
        
        w2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 70, 35, 15)];
        w2.font = [UIFont systemFontOfSize:12];
        w2.layer.cornerRadius = 2;
        w2.layer.borderColor = themeColor.CGColor;
        w2.layer.borderWidth = 1;
        w2.textAlignment = NSTextAlignmentCenter;
        w2.clipsToBounds = YES;
        w2.textColor = themeColor;
        [self addSubview:w2];
        
        w3 = [[UILabel alloc]initWithFrame:CGRectMake(90, 70, 35, 15)];
        w3.font = [UIFont systemFontOfSize:12];
        w3.layer.cornerRadius = 2;
        w3.layer.borderColor = themeColor.CGColor;
        w3.layer.borderWidth = 1;
        w3.textAlignment = NSTextAlignmentCenter;
        w3.clipsToBounds = YES;
        w3.textColor = themeColor;
        [self addSubview:w3];
        
        w4 = [[UILabel alloc]initWithFrame:CGRectMake(130, 70, 35, 15)];
        w4.font = [UIFont systemFontOfSize:12];
        w4.layer.cornerRadius = 2;
        w4.layer.borderColor = themeColor.CGColor;
        w4.layer.borderWidth = 1;
        w4.textAlignment = NSTextAlignmentCenter;
        w4.clipsToBounds = YES;
        w4.textColor = themeColor;
        [self addSubview:w4];
        
        w5 = [[UILabel alloc]initWithFrame:CGRectMake(170, 70, 35, 15)];
        w5.font = [UIFont systemFontOfSize:12];
        w5.layer.cornerRadius = 2;
        w5.layer.borderColor = themeColor.CGColor;
        w5.layer.borderWidth = 1;
        w5.textAlignment = NSTextAlignmentCenter;
        w5.clipsToBounds = YES;
        w5.textColor = themeColor;
        [self addSubview:w5];
        
        w6 = [[UILabel alloc]initWithFrame:CGRectMake(210, 70, 35, 15)];
        w6.font = [UIFont systemFontOfSize:12];
        w6.layer.cornerRadius = 2;
        w6.layer.borderColor = themeColor.CGColor;
        w6.layer.borderWidth = 1;
        w6.textAlignment = NSTextAlignmentCenter;
        w6.clipsToBounds = YES;
        w6.textColor = themeColor;
        [self addSubview:w6];
        
        w7 = [[UILabel alloc]initWithFrame:CGRectMake(250, 70, 35, 15)];
        w7.font = [UIFont systemFontOfSize:12];
        w7.layer.cornerRadius = 2;
        w7.layer.borderColor = themeColor.CGColor;
        w7.layer.borderWidth = 1;
        w7.textAlignment = NSTextAlignmentCenter;
        w7.clipsToBounds = YES;
        w7.textColor = themeColor;
        [self addSubview:w7];
        
        line = [[UIView alloc]initWithFrame:CGRectMake(0, 139, ScreenWidth, 1)];
        line.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [self addSubview:line];

    }
    return self;
}

+(CGFloat)heightWithType:(int)type
{
    if (type == 1)
    {
        return 140;
    }
    else if (type == 2)
    {
        return 140;
    }
    else if (type == 3)
    {
        return 140 - 25;
    }
    else if (type == 4)
    {
        return 140 - 25;
    }
    else
    {
        return 140;
    }
}

-(void)loadData:(NSDictionary *)data type:(int)type
{
    personType.hidden = YES;
    tags.hidden = YES;
    if (type == 1)
    {
        titleLabel.text = [data objectForKey:@"TaskAbilityName"];
        priceLabel.text = [NSString stringWithFormat:@"%@元/小时",[data objectForKey:@"TaskPrice"]];
        
        NSDate * start = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskBeginDay"]floatValue]];
        
        NSDate * end = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskEndDay"]floatValue]];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        NSString * startS = [dateFormatter stringFromDate:start];
        NSString * endS = [dateFormatter stringFromDate:end];

        timeLabel.text = [NSString stringWithFormat:@"%@-%@",startS,endS];
        [avatarImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[data objectForKey:@"UserPic"]]]];
        nameLabel.text = [data objectForKey:@"UserName"];
        
        w1.hidden = YES;
        w2.hidden = YES;
        w3.hidden = YES;
        w4.hidden = YES;
        w5.hidden = YES;
        w6.hidden = YES;
        w7.hidden = YES;

        switch ([[data objectForKey:@"TaskWeekDay"]count]) {
            case 7:
                w7.hidden = NO;
                w7.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:6];
            case 6:
                w6.hidden = NO;
                w6.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:5];
            case 5:
                w5.hidden = NO;
                w5.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:4];
            case 4:
                w4.hidden = NO;
                w4.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:3];
            case 3:
                w3.hidden = NO;
                w3.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:2];
            case 2:
                w2.hidden = NO;
                w2.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:1];
            case 1:
                w1.hidden = NO;
                w1.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:0];
            default:
                break;
        }
        
        
        avatarImage.frame = CGRectMake(10, 95, 40, 40);
        nameLabel.frame = CGRectMake(60, 95, ScreenWidth, 40);
        personType.frame = CGRectMake(ScreenWidth - 60, 105, 40, 20);
        
        line.frame = CGRectMake(0, 139, ScreenWidth, 1);
    }
    if (type == 2)
    {
        titleLabel.text = [data objectForKey:@"TaskAbilityName"];
        priceLabel.text = [NSString stringWithFormat:@"%@元/小时",[data objectForKey:@"TaskPrice"]];
        
        NSDate * start = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskBeginDay"]floatValue]];
        
        NSDate * end = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskEndDay"]floatValue]];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        NSString * startS = [dateFormatter stringFromDate:start];
        NSString * endS = [dateFormatter stringFromDate:end];
        
        timeLabel.text = [NSString stringWithFormat:@"%@-%@",startS,endS];
        [avatarImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[data objectForKey:@"UserPic"]]]];
        nameLabel.text = [data objectForKey:@"UserName"];
        
        w1.hidden = YES;
        w2.hidden = YES;
        w3.hidden = YES;
        w4.hidden = YES;
        w5.hidden = YES;
        w6.hidden = YES;
        w7.hidden = YES;
        
        switch ([[data objectForKey:@"TaskWeekDay"]count]) {
            case 7:
                w7.hidden = NO;
                w7.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:6];
            case 6:
                w6.hidden = NO;
                w6.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:5];
            case 5:
                w5.hidden = NO;
                w5.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:4];
            case 4:
                w4.hidden = NO;
                w4.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:3];
            case 3:
                w3.hidden = NO;
                w3.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:2];
            case 2:
                w2.hidden = NO;
                w2.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:1];
            case 1:
                w1.hidden = NO;
                w1.text = [[data objectForKey:@"TaskWeekDay"]objectAtIndex:0];
            default:
                break;
        }
        
        
        avatarImage.frame = CGRectMake(10, 95, 40, 40);
        nameLabel.frame = CGRectMake(60, 95, ScreenWidth, 40);
        personType.frame = CGRectMake(ScreenWidth - 60, 105, 40, 20);
        
        line.frame = CGRectMake(0, 139, ScreenWidth, 1);
    }
    if (type == 3)
    {
        titleLabel.text = [data objectForKey:@"TaskTitle"];
        priceLabel.text = @"";

        NSDate * start = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskBeginTime"]floatValue]];
        
        NSDate * end = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskEndTime"]floatValue]];
        
        tags.hidden = NO;
        if ([start compare:[NSDate date]] == NSOrderedAscending)
        {
            if ([end compare:[NSDate date]] == NSOrderedAscending) {
                //结束
                tags.text = @"结束";
            }
            else
            {
                // 进行
                tags.text = @"结束";
            }
        }
        else
        {
            //未开始
            tags.text = @"结束";
        }
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        NSString * startS = [dateFormatter stringFromDate:start];
        NSString * endS = [dateFormatter stringFromDate:end];
        
        timeLabel.text = [NSString stringWithFormat:@"%@-%@",startS,endS];
        [avatarImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[data objectForKey:@"UserPic"]]]];
        nameLabel.text = [data objectForKey:@"UserName"];
        
        w1.hidden = YES;
        w2.hidden = YES;
        w3.hidden = YES;
        w4.hidden = YES;
        w5.hidden = YES;
        w6.hidden = YES;
        w7.hidden = YES;
        
        personType.hidden = NO;
        personType.text = [data objectForKey:@"PublisherType"];
        
        avatarImage.frame = CGRectMake(10, 70, 40, 40);
        nameLabel.frame = CGRectMake(60, 70, ScreenWidth, 40);
        personType.frame = CGRectMake(ScreenWidth - 60, 80, 40, 20);
        
        line.frame = CGRectMake(0, 140-26, ScreenWidth, 1);
    }
    if (type == 4)
    {
        titleLabel.text = [data objectForKey:@"TaskTitle"];
        priceLabel.text = [data objectForKey:@"TaskPrice"];
        
        NSDate * start = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskBeginTime"]floatValue]];
        
        NSDate * end = [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:@"TaskEndTime"]floatValue]];
        
//        if ([start compare:[NSDate date]] == NSOrderedAscending)
//        {
//            if ([end compare:[NSDate date]] == NSOrderedAscending) {
//                //结束
//                tags.text = @"结束";
//            }
//            else
//            {
//                // 进行
//                tags.text = @"结束";
//            }
//        }
//        else
//        {
//            //未开始
//            tags.text = @"结束";
//        }
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
        
        NSString * startS = [dateFormatter stringFromDate:start];
        NSString * endS = [dateFormatter stringFromDate:end];
        
        timeLabel.text = [NSString stringWithFormat:@"%@-%@",startS,endS];
        [avatarImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://pic.xrsquare.com/%@",[data objectForKey:@"UserPic"]]]];
        nameLabel.text = [data objectForKey:@"UserName"];
        
        w1.hidden = YES;
        w2.hidden = YES;
        w3.hidden = YES;
        w4.hidden = YES;
        w5.hidden = YES;
        w6.hidden = YES;
        w7.hidden = YES;
        
        personType.hidden = NO;
        personType.text = @"企业";
        
        avatarImage.frame = CGRectMake(10, 70, 40, 40);
        nameLabel.frame = CGRectMake(60, 70, ScreenWidth, 40);
        personType.frame = CGRectMake(ScreenWidth - 60, 80, 40, 20);
        
        line.frame = CGRectMake(0, 140-26, ScreenWidth, 1);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
